<template>
	<view class="container">
		<uni-forms ref="form" :rules="{}">

			<uni-forms-item label="" name="">
				<uni-easyinput v-model="data.comment" type="textarea" autoHeight placeholder="请输入评论内容" />
			</uni-forms-item>

			<uni-forms-item label="" name="">
				<button type="primary" @click="postComment">发表</button>
			</uni-forms-item>

		</uni-forms>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				data: {
					comment: '',
					foodId: null,
					postDate: null,
					userId: null
				}
			}
		},
		methods: {
			postComment() {
				// 获取提交时间
				let date = new Date() // 获取当前时间
				let time = date.toISOString() // 必须以 - 分开
				// console.log(time);  // 2024-05-23T03:02:43.857Z
				this.data.postDate = time
				uni.request({
					url: 'http://localhost:8090/foodapp/comments/add',
					method: 'POST',
					data: this.data,
					success: res => {
						console.log(res.data.data);
						if (res.data.success) {
							uni.showToast({
								title: '发表成功',
								icon: 'none'
							});
						} else {
							uni.showToast({
								title: '失败',
								icon: 'none'
							});
						}
					},
					fail: () => {},
					complete: () => {}
				});

			}
		},

		onLoad(e) {
			// console.log(e);
			// 获取待评价 食物ID
			this.data.foodId = e.foodid || 1
			// 获取当前 用户ID
			this.data.userId = uni.getStorageSync('userinfo').id
			// console.log(this.data.userId);
		}
	}
</script>

<style>

</style>